#include <cstdint>
#include <cstdio>
#include <deque>
#include <iostream>
#include <istream>
using ll = int64_t;
#define sl static inline
const ll maxn = 505;
ll n,m,x1,y1,x2,y2,inf=1e9+7;
char c[maxn][maxn];
ll ans[maxn][maxn];

struct P{
    ll x,y,s;
};

ll dis[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

sl int solve(){
    std::cin>>n>>m;
    if(n==0 && m==0)return 1;
    for(ll i=0;i<n;i++){
        for(ll j=0;j<m;j++){
            std::cin>>c[i][j];
            ans[i][j]=inf;
        }
    }
    // printf("line = %d\n",__LINE__);
    std::cin>>x1>>y1>>x2>>y2;
    std::deque<P> dq;
    dq.emplace_back(x1,y1,0);
    while(dq.size()){
        auto [x,y,s] = dq.front();
        dq.pop_front();
        for(ll i=0;i<4;i++){
            ll nx=x+dis[i][0],ny=y+dis[i][1];
            if(nx<0||nx>n-1||ny<0||ny>m-1)continue;
            ll ns = s+(c[x][y]!=c[nx][ny]);
            if(nx==x2&&ny==y2){
                std::cout<<ns<<"\n";
                return 0;
            }
            if(ns<ans[nx][ny]){
                ans[nx][ny]=ns;
                if(c[x][y]!=c[nx][ny]){
                    dq.emplace_back(nx,ny,ns);
                }else{
                    dq.emplace_front(nx,ny,s);
                }
            }
        }
    }

    return 0;
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    while (!solve());
}